package org.digitarts.rtms.spring.collectors.jms;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.digitarts.rtms.helpers.FileHelper;

public class ByteArraySyntheticMessageFactory extends SimpleSyntehticMessageFactory
{
	protected URL		byteSourceURL	= null;
	protected byte[]	payload			= null;

	public ByteArraySyntheticMessageFactory(URL byteSourceURL)
	{
		this.byteSourceURL = byteSourceURL;
		payload = FileHelper.getBytesFromUrl(byteSourceURL);
	}

	@Override
	public String getName()
	{
		return name + payload.length;
	}

	@Override
	public Message[] createMessage(Session session) throws JMSException
	{
		List<Message> messages = new ArrayList<Message>(messageCount);
		for (int i = 0; i < messageCount; i++)
		{
			BytesMessage message = session.createBytesMessage();
			message.writeBytes(payload);
			messages.add(message);
		}
		return messages.toArray(new Message[messages.size()]);
	}
}
